package com.example.mapper;

import com.example.VO.BorrowRecordVO;
import com.example.entity.BorrowRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @auth shanyue
 * @date 2025年06月06日15:54
 */
@Mapper
public interface BorrowRecordMapper {

    @Insert("INSERT INTO borrow_records (user_id, book_id, borrow_date) VALUES (#{userId}, #{bookId}, #{borrowDate})")
    void insert(BorrowRecord record);

    @Update("UPDATE borrow_records SET return_date = NOW() WHERE id = #{id}")
    int returnBook(Integer id);

    @Results({
            @Result(column = "borrowDate", property = "borrowTime"),
            @Result(column = "return_date", property = "returned")
    })
    @Select("""
            SELECT br.id, b.title AS bookTitle, br.borrow_date AS borrowDate, br.return_date
            FROM borrow_records br
            JOIN books b ON br.book_id = b.id
            WHERE br.user_id = #{userId}
            ORDER BY br.borrow_date DESC
            """)
    List<BorrowRecordVO> findByUserId(Integer userId);

    @Select("SELECT book_id FROM borrow_records WHERE id = #{recordId}")
    Integer getBookIdByRecordId(@Param("recordId") Integer recordId);
}
